<template>
	<view class="container" >
		<!-- 导航栏 -->
		<uni-nav-bar dark :fixed="true" shadow background-color="#007AFF" status-bar left-icon="left" left-text="返回"
			titleStyle="{height: '52px',font-size:15px}" :title="sample.name" @clickLeft="back">
			<block slot="right">
				<view class="city">
					<text style="font-size: 15px;" @click="submit">提交</text>
				</view>
			</block>
		</uni-nav-bar>
		<uni-section title="处置信息" type="line">
			<view class="example">
				<!-- 基础用法，不包含校验规则 -->
				<uni-forms labelPosition="top" >
					<uni-forms-item label="处置人" labelPosition="top">
						<uni-combox :candidates="userNames" v-on:input="userChanged" placeholder="请选择领样人"
							v-model="sample.handlerName" ></uni-combox>
					</uni-forms-item>
					<uni-forms-item label="处置时间" >
						<uni-datetime-picker type="datetime" v-model="sample.handleTime" />
					</uni-forms-item>
					<uni-forms-item label="处置方式" >
						<uni-data-checkbox v-model="sample.handleStrategy"  :localdata="handleStrategy" mode="button" @change="changeReturnLocation"/>
					</uni-forms-item>
					<uni-forms-item label="其他说明" >
						<uni-easyinput type="textarea" autoHeight v-model="sample.handleComment" placeholder="请输入退回是由说明"></uni-easyinput>
					</uni-forms-item>
				</uni-forms>
			</view>
		</uni-section>
		<u-modal :show="showConfirmModal" title="样品处置申请确认" :showCancelButton="true"
			 :content="'确认将样品: ['+sample.name+']\n提交处置审核?'" @cancel="closeModal"
			 @confirm="confirm" :asyncClose="false">
		</u-modal>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				reason:'',
				sample:{},
				showConfirmModal:false,
				userNames:[],
				locations:[],
				users:[]
			}
		},
		onLoad(option) {
			// 加载样品存放地点(字典数据，Location)
			this.loadSampleInfo(option.id);
			this.loadDictData('handle_strategy');
			this.queryUserList(this.vuex_user.deptId);
		},
		methods: {
			queryUserList(orgId) {
				var this_ = this; //存储this
				this.$u.api.queryOrgUserList(orgId).then((response) => {
					//这个控件只能支持字符串选取
					this_.users = response.rows;
					let userNames = [response.rows.length];
					for (let index = 0; index < response.rows.length; index++) {
						userNames[index] = response.rows[index].nickName;
					}
					this_.userNames = userNames;
				});
			},
			userChanged(userName) {
				//如果填入的用户名称非法，则主动清空内容
				var userId = this.resolveUserIdWithSelectedUserName(userName);
			
				if (!userId) {
					this.sample.handlerName = '';
				} else {
					this.sample.handlerName = userName;
					this.sample.handler = userId;
				}
			},
			resolveUserIdWithSelectedUserName(userName) {
				for (let index = 0; index < this.users.length; index++) {
					if (this.users[index].userName === userName) return this.users[index].userId;
				}
				return null;
			},
			loadDictData(dictType) {
				var this_ = this; //存储this
				this.$u.api.getDictData({
					"dictType": dictType
				}).then((response) => {
					let handleStrategy = [response.data.length];
					for (let index = 0; index < response.data.length; index++) {
						handleStrategy[index] = {
							'text': response.data[index].dictLabel,
							'value': response.data[index].dictValue
						};
					}
					handleStrategy[handleStrategy.length] = {
						'text': '其他地点',
						'value': 'custom'
					};
					this_.sample.handleStrategy = handleStrategy[0].value;
					this_.handleStrategy = handleStrategy;
				});
			},
			loadSampleInfo(sampleId){
				var this_ = this;
				this.$u.api.querySampleInfo(sampleId).then(res=>{
					this_.sample = res.data;
					this_.sample.handler = this_.vuex_user.userId;
					this_.sample.handlerName = this_.vuex_user.nickName;
				});
			},
			submit(){
				this.showConfirmModal = true;
			},
			confirm(){
				var this_ = this; //存储this
				this.$u.api.forwardSample(this_.sample).then(res=>{
					this_.$u.toast('处置提交成功');
					this_.showConfirmModal = false;
					this_.back();
				});
			},
			closeModal(){
				this.showConfirmModal = false;
			},
			back(){
				this.backPageRefresh();
			}
		}
	}
</script>

<style>
	.uni-section {
		margin-top: 0px !important;
		background-color: #eee;
	}

	.example {
		background-color: white;
		padding: 10px 20px 15px 20px;
	}

	.segmented-control {
		margin-bottom: 10px;
	}

	.button-group {
		margin-top: 15px;
		display: flex;
		justify-content: space-around;
	}

	.form-item {
		display: flex;
		align-items: center;
	}

	.button {
		display: flex;
		align-items: center;
		height: 35px;
		margin-left: 10px;
	}
</style>
